package com.settlement.system.service.impl;

import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblCustomizedExporeDtlMapper;
import com.settlement.system.mapper.TblCustomizedExporeMainMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.CustomizedExporeParams;
import com.settlement.system.model.entity.common.TblCustomizedExporeDtl;
import com.settlement.system.model.entity.common.TblCustomizedExporeMain;
import com.settlement.system.service.ITblCustomizedExporeMainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 自定义导出主表 服务实现类
 *
 * @author Ximi
 * @since 2020-11-17
 */
@Service
@Slf4j
public class TblCustomizedExporeMainServiceImpl extends BaseServiceImpl implements ITblCustomizedExporeMainService {

    @Resource
    private TblCustomizedExporeMainMapper tblCustomizedExporeMainMapper;

    @Resource
    private TblCustomizedExporeDtlMapper tblCustomizedExporeDtlMapper;

    @Override
    public BaseMapper init() {
        return tblCustomizedExporeMainMapper;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCustomizedExpore(CustomizedExporeParams params) throws ServiceException {
        try {
            TblCustomizedExporeMain exporeMain = tblCustomizedExporeMainMapper
                    .getByBothName(params.getMenuName(),params.getCustomizedExporeName());
            if (exporeMain != null) {
                throw new ServiceException("自定义名称已经存在");
            }

            TblCustomizedExporeMain tmp = new TblCustomizedExporeMain();
            tmp.setCustomizedExporeName(params.getCustomizedExporeName());
            tmp.setMenuName(params.getMenuName());
            SystemUserUtil.setDefualt(tmp);
            saveCustomizedExporeDtl(params, tmp.getId());
            tblCustomizedExporeMainMapper.insert(tmp);
        } catch (Exception ex) {
            log.error("保持自定义导出异常",ex);
            throw new ServiceException("系统异常", ex);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void updateCustomizedExpore(CustomizedExporeParams params) throws ServiceException {
        try {
            tblCustomizedExporeMainMapper.updateCustomizedExporeNameById(params.getCustomizedExporeName(),params.getMainId());
            tblCustomizedExporeDtlMapper.deleteByMainId(params.getMainId());
            saveCustomizedExporeDtl(params, params.getMainId());
        } catch (Exception ex) {
            log.error("更新自定义导出异常",ex);
            throw new ServiceException("系统异常", ex);
        }
    }

    /**
     * 保持自定义导出的明细
     *
     * @param params
     * @param mainId
     */
    private void saveCustomizedExporeDtl(CustomizedExporeParams params, String mainId) {
        if (null != params.getFields()) {
            for (int index = 0; index < params.getFields().size(); index++) {
                CustomizedExporeParams.CustomizedField customizedField = params.getFields().get(index);
                TblCustomizedExporeDtl exporeDtl = new TblCustomizedExporeDtl();
                exporeDtl.setFieldName(customizedField.getFieldName());
                exporeDtl.setFieldDescription(customizedField.getFieldDescription());
                exporeDtl.setMainId(mainId);
                exporeDtl.setPriority(index);
                SystemUserUtil.setDefualt(exporeDtl);
                tblCustomizedExporeDtlMapper.insert(exporeDtl);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteCustomizedExpore(CustomizedExporeParams params) throws ServiceException {
        try {
            tblCustomizedExporeMainMapper.deleteById(params.getMainId());
            tblCustomizedExporeDtlMapper.deleteByMainId(params.getMainId());
        } catch (Exception ex) {
            throw new ServiceException("系统异常", ex);
        }
    }

    @Override
    public List<TblCustomizedExporeDtl> getByBothName(String menuName, String customizedExporeName) throws ServiceException {
        try {
            TblCustomizedExporeMain exporeMain = tblCustomizedExporeMainMapper
                    .getByBothName(menuName, customizedExporeName);
            if (exporeMain != null) {
                List<TblCustomizedExporeDtl> exporeDtls = tblCustomizedExporeDtlMapper.getByMainId(exporeMain.getId());
                if (!CollectionUtils.isEmpty(exporeDtls)) {
                    return exporeDtls;
                }
            }
            return null;
        } catch (Exception ex) {
            log.error("获取自定义导出列表异常", ex);
            throw new ServiceException("获取自定义导出列表异常",ex);
        }
    }

    @Override
    public List<String> getCustomizedExporeNameByMenuName(String menuName) throws ServiceException {
        try {
            return tblCustomizedExporeMainMapper.getCustomizedExporeNameByMenuName(menuName);
        } catch (Exception ex){
            log.error("获取自定义导出的名称异常", ex);
            throw new ServiceException("获取自定义导出的名称异常",ex);
        }
    }

}

